package cn.itcast.jd.task;

import cn.itcast.jd.pojo.Item;
import cn.itcast.jd.service.ItemServiceImpl;
import cn.itcast.jd.util.HttpUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
@Component
public class ItemTask {
    private long count=0;
    private static final ObjectMapper MAPPER =  new ObjectMapper();
    @Autowired
    private ItemServiceImpl itemService;
    @Autowired
    private HttpUtils httpUtils;
    @Scheduled(fixedDelay = 10000*10000)
    public void itemTask()throws Exception{
        String url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&pvid=e690e738145d4b21a99332352c88817f&s=1&click=1&page=";
        for (int i = 1; i < 10; i+=2) {
            String html=httpUtils.doGetHtml(url+i);
            parse(html);
        }
        System.out.println("手机数据抓取成功！");
    }
    public void parse(String html) throws IOException {
        Document doc= Jsoup.parse(html);
        Elements elements= doc.select("div#J_goodsList > ul > li");
        for (Element spuEle:elements) {
            long spu=0;
            if (spuEle.attr("data-spu")!=null&&!"".equals(spuEle.attr("data-spu"))){
                spu=Long.parseLong(spuEle.attr("data-spu"));
            }else {
                spu=++count;
            }
            Elements skuEles=spuEle.select("li.ps-item");
            for (Element skuEle:skuEles) {
               Long sku=Long.parseLong(skuEle.select("[data-sku]").attr("data-sku"));
               Item item=new Item();
               item.setSku(sku);
               List<Item> list=itemService.findAll(item);
               if (list.size()>0){
                   continue;
               }
               item.setSpu(spu);
               String itemUrl="http://item.jd.com/"+sku+".html";
               item.setUrl(itemUrl);
               String picUrl="https:"+skuEle.select("img[data-sku]").first().attr("data-lazy-img");
               picUrl=picUrl.replace("/n9/","/n1/");
               String picName=this.httpUtils.doGetImage(picUrl);
               item.setPic(picName);
               String priceJson = this.httpUtils.sendGet("https://p.3.cn/prices/mgets?skuIds=J_" +sku);
               System.out.println(priceJson);
//               double price =MAPPER.readTree(priceJson).get(0).get("p").asDouble();
//               item.setPrice(price);
//               String itemInfo=httpUtils.doGetHtml(itemUrl);
//               String title=Jsoup.parse(itemInfo).select("div.sku-name").text();
//               item.setTitle(title);
//               item.setCreated(new Date());
//               item.setUpdated(item.getCreated());
//               itemService.save(item);
//                System.out.println("price:"+price);
            }
        }
    }

}
